/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sourcery;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import sourceryobjects.sRelationType;

/**
 *
 * @author claire
 */
public class RelationTypeEditor extends JDialog {
    private sRelationType editing;
    private MainPanel panel;
    public RelationTypeEditor (MainPanel panel, sRelationType editing) {
        this.editing = editing;
        this.panel = panel;
    }

    public void edit() {
        final JDialog dialog = new JDialog((JFrame) panel.getTopLevelAncestor(), "Editing Relation Type: " + editing.getName());
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
            JPanel controlPanel = new JPanel();
                JPanel rowPanel = new JPanel();
                    rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.LINE_AXIS));
                    rowPanel.add(new JLabel("Name:"));
                    JTextField nameField = new JTextField(20);
                    nameField.setText(editing.getName());
                    nameField.getDocument().addDocumentListener(new DocumentListener(){
                        public void insertUpdate(DocumentEvent e) { changedUpdate(e); }
                        public void removeUpdate(DocumentEvent e) { changedUpdate(e); }
                        public void changedUpdate(DocumentEvent e) {
                            Document d = (Document)e.getDocument();
                            try {
                                editing.setName(d.getText(0, d.getLength()));
                            } catch (BadLocationException ex) {
                                Logger.getLogger(RelationTypeEditor.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });
                    rowPanel.add(nameField);
                controlPanel.add(rowPanel);

                // Length controls
                rowPanel = new JPanel();
                    rowPanel.add(new JLabel("Length:"));
                    SpinnerNumberModel spinModel = new SpinnerNumberModel(editing.getMinLength(), 0, 1000, 5);
                        spinModel.addChangeListener(new ChangeListener() {
                            public void stateChanged(ChangeEvent e) {
                                SpinnerNumberModel mode = (SpinnerNumberModel)e.getSource();
                                editing.setMinLength(mode.getNumber().doubleValue());
                            }
                        });
                    rowPanel.add(new JSpinner(spinModel));
                    rowPanel.add(new JLabel("-"));
                    spinModel = new SpinnerNumberModel(editing.getMaxLength(), 0, 1000, 5);
                        spinModel.addChangeListener(new ChangeListener() {
                            public void stateChanged(ChangeEvent e) {
                                SpinnerNumberModel mode = (SpinnerNumberModel)e.getSource();
                                editing.setMaxLength(mode.getNumber().doubleValue());
                            }
                        });
                    rowPanel.add(new JSpinner(spinModel));

                controlPanel.add(rowPanel);

                // Pull strength controls
                rowPanel = new JPanel();
                    rowPanel.add(new JLabel("Strength:"));
                    spinModel = new SpinnerNumberModel(editing.getStrengthA(), 0, 10, 0.05);
                        spinModel.addChangeListener(new ChangeListener() {
                            public void stateChanged(ChangeEvent e) {
                                SpinnerNumberModel mode = (SpinnerNumberModel)e.getSource();
                                editing.setStrengthA(mode.getNumber().doubleValue());
                            }
                        });
                    rowPanel.add(new JSpinner(spinModel));
                    rowPanel.add(new JLabel("-"));
                    spinModel = new SpinnerNumberModel(editing.getStrengthB(), 0, 10, 0.05);
                        spinModel.addChangeListener(new ChangeListener() {
                            public void stateChanged(ChangeEvent e) {
                                SpinnerNumberModel mode = (SpinnerNumberModel)e.getSource();
                                editing.setStrengthB(mode.getNumber().doubleValue());
                            }
                        });
                    rowPanel.add(new JSpinner(spinModel));
                controlPanel.add(rowPanel);

                // Angle controls
                rowPanel = new JPanel();
                    rowPanel.add(new JLabel("Angle:"));
                    spinModel = new SpinnerNumberModel(editing.getMinAngle(), 0, 360, 45);
                        spinModel.addChangeListener(new ChangeListener() {
                            public void stateChanged(ChangeEvent e) {
                                SpinnerNumberModel mode = (SpinnerNumberModel)e.getSource();
                                editing.setMinAngle(mode.getNumber().doubleValue());
                            }
                        });
                    rowPanel.add(new JSpinner(spinModel));
                    rowPanel.add(new JLabel("-"));
                    spinModel = new SpinnerNumberModel(editing.getMaxAngle(), 0, 360, 45);
                        spinModel.addChangeListener(new ChangeListener() {
                            public void stateChanged(ChangeEvent e) {
                                SpinnerNumberModel mode = (SpinnerNumberModel)e.getSource();
                                editing.setMaxAngle(mode.getNumber().doubleValue());
                            }
                        });
                    rowPanel.add(new JSpinner(spinModel));
                controlPanel.add(rowPanel);
            contentPanel.add(controlPanel, BorderLayout.CENTER);

            JPanel buttonPanel = new JPanel();
                JButton closeButton = new JButton("Done");
                closeButton.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       dialog.setVisible(false);
                       dialog.dispose();
                       panel.editRelations();
                   }
                });
                buttonPanel.add(closeButton);
            contentPanel.add(buttonPanel, BorderLayout.SOUTH);
        dialog.setContentPane(contentPanel);

        dialog.setSize(new Dimension(300, 300));
        dialog.setVisible(true);
    }

}
